package com.itheima.test02;

import com.itheima.bean.Classes;
import com.itheima.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestOneToMany {
    /**
     * @throws Exception
     * 班级及学生
     */
    @Test
    public void selectAllByClass() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        ClassToStuMapper classMapper = sqlSession.getMapper(ClassToStuMapper.class);
        List<Classes> classesList = classMapper.selectAll();
        for (Classes classes : classesList) {
            System.out.println(classes.toString());
        }
        sqlSession.close();
        is.close();
    }

    /**
     * @throws Exception
     * 学生及所在班级
     */
    @Test
    public void selectAllByStu() throws Exception{
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        StuToClassMapper mapper = sqlSession.getMapper(StuToClassMapper.class);
        List<Student> students = mapper.selectAll();
        for (Student student : students) {
            System.out.println(student);
        }

        sqlSession.close();
        is.close();
    }

}
